/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerAppService
 * Author:   Allen
 * Date:     2020/8/26 21:53
 * Description: 商家应用接入号登记处理服务类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.openapi.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bim.marvel.openapi.client.constant.OpenApiCodeEnum;
import com.bim.marvel.openapi.core.dao.DealerAppDAO;
import com.bim.marvel.openapi.core.entity.DealerAppDO;
import com.bim.marvel.openapi.core.entity.DealerCompanyDO;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.exception.ServiceException;
import com.bim.marvel.openapi.client.query.AppDetailQuery;
import com.bim.marvel.openapi.client.query.DealerAppQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 〈商家应用接入号登记处理服务类〉
 *
 * @author Allen
 * @date 2020/8/26
 * @since 1.0.0
 */
@Service
public class DealerAppService extends ServiceImpl<DealerAppDAO, DealerAppDO> {

    /**
     * 注入DealerCompanyService
     */
    @Autowired
    private DealerCompanyService dealerCompanyService;

    /**
     * 注入AppDetailService
     */
    @Autowired
    private AppDetailService appDetailService;

    /**
     * 保存商家应用接入号信息
     * @param dealerAppQuery 商家应用接入号信息入参
     * @return 保存状态
     */
    public Boolean saveDealerApp(DealerAppQuery dealerAppQuery) {
        if (StringUtils.isEmpty(dealerAppQuery.getCompanyCode())) {
            throw new ServiceException(CodeEnum.PARAMS_ERROR);
        }
        DealerCompanyDO dealerCompanyDO = dealerCompanyService.getById(dealerAppQuery.getCompanyCode());
        if (dealerCompanyDO == null) {
            throw new ServiceException(CodeEnum.DATA_NOT_FOUND);
        }
        DealerAppDO dealerAppDO = new DealerAppDO();
        dealerAppDO.setCompanyCode(dealerAppQuery.getCompanyCode());
        dealerAppDO.setAppType(dealerAppQuery.getAppType());
        QueryWrapper queryWrapper = new QueryWrapper(dealerAppDO);
        Boolean exists = SqlHelper.retBool(count(queryWrapper));
        if (exists) {
            throw new ServiceException(OpenApiCodeEnum.DEALER_APP_REPETITION_ERROR);
        }
        BeanUtils.copyProperties(dealerAppQuery, dealerAppDO);
        //如果用户不填写，可由系统自动生成
        if (dealerAppDO.getAppId() == null) {
            dealerAppDO.setAppId(IdWorker.get32UUID());
        }
        dealerAppDO.setReviewStatus(false);
        return save(dealerAppDO);
    }

    /**
     * 审核商家应用接入号信息
     * @param appId 商家应用接入号入参
     * @param status 审核通过状态
     * @return 审核结果
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean reviewDealerApp(String appId, Boolean status) {
        DealerAppDO dealerAppDO = new DealerAppDO();
        dealerAppDO.setAppId(appId);
        dealerAppDO.setReviewStatus(status);
        updateById(dealerAppDO);
        AppDetailQuery appDetailQuery = new AppDetailQuery();
        appDetailQuery.setAppId(appId);
        appDetailQuery.setAppSecret(IdWorker.getIdStr());
        appDetailQuery.setSessionKey(IdWorker.get32UUID());
        //TODO 这里由于无法直接调用Auth的服务，故而Token只能人工添加,通过前台页面完成更新操作
        return appDetailService.saveAppDateil(appDetailQuery);
    }

    /**
     * 根据当前用户的企业编号获得该企业申请的商户接入号列表（无须分页）
     * @param companyCode 企业编号
     * @return 商户接入号列表
     */
    public List<DealerAppDO> listDealerApp(String companyCode) {
        QueryWrapper<DealerAppDO> queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_code", companyCode);
        return list(queryWrapper);
    }
}
